#include <vector>
#include <string>

using namespace std;

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {

        int n = gas.size();
        int sum = 0;
        for (int i = 0; i < n; ++i)
        {
            sum += gas[i] - cost[i];
        }
        if (sum < 0) return -1;

        sum = gas[0] - cost[0];
        int start = 0;
        for (int i = 1; i < n; ++i)
        {
            if (sum < 0)
            {
                sum = 0;
                start = i;
            }
            sum += gas[i] - cost[i];

        }

        return start;
    }
};
